#!/bin/bash
#
# Startup script for captagent
#
# chkconfig: 345 85 15
# description: captagent - the Open Source Homer Capture Agent
#
# processname: captagent
# pidfile: /var/run/captagent.pid
# config: /usr/local/etc/captagent/captagent.xml
#
### BEGIN INIT INFO
# Provides: captagent
# Required-Start: $local_fs $network
# Short-Description: captagent - the Open Source Homer Capture Agent
# Description: Homer captagent is an Open Source Capture Programm released
# 	under GPLv3, able to handle thousands of call setups per second.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

prog=captagent
APP_FILE=/usr/local/bin/$prog
PID_FILE=/var/run/$prog.pid
LOCK_FILE=/var/lock/subsys/$prog
RETVAL=0

[ -z "$CFG_FILE" ]  && CFG_FILE=/usr/local/captagent/etc/captagent/captagent.xml

OPTIONS="-f $CFG_FILE"


[ -f /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

start() {
  if [ -e $PID_FILE ]; then
        echo "[FAILED] Captagent is already running with PID: `cat $PID_FILE`"
  else  
        echo -n $"Starting $prog: "
         # there is something at end of this output which is needed to
         # report proper [ OK ] status in CentOS scripts
         $APP_FILE $OPTIONS && success || failure
         RETVAL=$?
         echo
         [ $RETVAL = 0 ] && touch $LOCK_FILE
  fi       
}

stop() {
	echo -n $"Stopping $prog: "
	killproc $APP_FILE
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f $LOCK_FILE $PID_FILE
}

status() {
  if [ -e $PID_FILE ]; then
             echo "[OK] Captagent is running with PID: `cat $PID_FILE`"
  else
             echo "[FAILED] $PID_FILE does not exist"
             RETVAL=1
  fi   
	return $RETVAL
}


# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status
		;;
	restart)
		stop
		start
		;;
	condrestart)
		if [ -f $PID_FILE ] ; then
			stop
			start
		fi
		;;
	*)
		echo $"Usage: $prog {start|stop|restart|condrestart|status|help}"
		exit 1
esac

exit $RETVAL

